#include "moneydelegate.h"
//#include <QDoubleSpinBox>
#include <QItemDelegate>

MoneyDelegate::MoneyDelegate(QObject *parent, bool zero) : QItemDelegate(parent)
{
    zeroIfNull = zero;
}

void MoneyDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const {

         double prix = index.data(Qt::DisplayRole).toDouble();
         //QString prixtexte = QString("%1 €").arg(prix,10,'f',2,QChar(' '));
         QString prixtexte = QString("%1 €").arg(prix,9,'f',2);
         if(!zeroIfNull && prix == 0){
             prixtexte = "";
         }
         QStyleOptionViewItem myoption = option;
         myoption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
         QPalette *mapalette = new QPalette();
         if (prix<0) { mapalette->setColor(QPalette::Text,Qt::red);
                       mapalette->setColor(QPalette::HighlightedText,Qt::red);}
         if (prix==0) { mapalette->setColor(QPalette::Text,Qt::darkGray);
                        mapalette->setColor(QPalette::HighlightedText,Qt::darkGray);}
         if (prix>0) { mapalette->setColor(QPalette::Text,Qt::darkGreen);
                       mapalette->setColor(QPalette::HighlightedText,Qt::darkGreen);}
         myoption.palette = *mapalette;

         drawDisplay(painter, myoption, myoption.rect , prixtexte);
         //drawFocus(painter, myoption, myoption.rect);

}

QSize MoneyDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSize size = option.fontMetrics.boundingRect(QString("%1 €").arg(index.data().toDouble(),9,'f',2)).size();
    size.setWidth(size.width()+15);
    return size;
}




